<template>
    <div :id="window.getRootName() + 'SIMPLEATTR'" class="attribute-text main-panel-wrap-b">
    </div>
</template>

<script>
import store from '@/js/store/state.js';
import util from '@/js/util/util.js';
import JQuery from 'jquery';
import config from '@/js/util/config.js';
import editor from '@/js/main/editor.js';
import * as echarts from 'echarts';
import system from '@/js/main/system.js';

export default {
    data() {
        return {
            window: window,
            currentElementList: store.getCurrentElementList(),
            elementList: store.getElementSortList()
        };
    },
    mounted() {
        this.createElementAttr();
    },
    methods: {
        createElementAttr() {
            if (this.currentElementList[0].data.file) {
                let file = null;
                for (let i = 0; i < this.currentElementList[0].data.file.length; i++) {
                    if (this.currentElementList[0].data.file[i].name == 'elementAttr.html') {
                        file = this.currentElementList[0].data.file[i].file;
                        break;
                    }
                }
                if (file) {
                    let _this = this;
                    let url = '';
                    if (this.mode == 3) {
                        url = 'simple/' + this.currentElementList[0].data.id + '/elementAttr.html';
                    } else {
                        url = `${config.getServiceURL('scene')}service/scene/element/file/one`;
                    }
                    JQuery.ajax({
                        url: url,
                        type: 'POST',
                        data: {
                            file: file
                        },
                        success: function(responseText) {
                            let html = responseText;
                            let id = util.replaceMinus(system.SYSTEM_EDIT_ELEMENT + _this.currentElementList[0].id);
                            html = html.replace(/{id}/g, id);
                            for (let i = 0; i < _this.currentElementList[0].data.file.length; i++) {
                                if (_this.currentElementList[0].data.file[i].name !== 'elementAttr.html') {
                                    let url = '';
                                    if (_this.mode == 3) {
                                        url = _this.currentElementList[0].data.id + '/' + _this.currentElementList[0].data.file[i].name;
                                    } else {
                                        url = `${config.getServiceURL('scene')}service/scene/file/download?file=${_this.currentElementList[0].data.file[i].file}`;
                                    }
                                    html = html.replace(new RegExp('"' + _this.currentElementList[0].data.file[i].name + '"', 'g'), '"' + url + '"');
                                }
                            }
                            JQuery('#' + window.getRootName() + 'SIMPLEATTR').html(html);
                            window['setElementAttr']({
                                elementList: _this.elementList,
                                target: _this.currentElementList[0],
                                editor: editor,
                                JQuery: JQuery,
                                echarts: echarts
                            });
                        }
                    });
                }
            }
        }
    },
    destroyed() {}
};
</script>